const { Student, ImgSchema, Claxx, School, Score } = require("../model");
const multer = require("multer");
const express = require("express");
const router = express.Router();

//注销
router.post("/delete", async (q, s) => {
  const res = await Student.deleteOne(q.body); // 删除第一个匹配的
  s.send({
    success: true,
    data: res,
  });
});

// 注册 - 登录
router.post("/login", async (q, s) => {
  const doc = await Student.findOne(q.body); // 返回找到的第一个
  if (doc) {
    s.send({
      msg: "登录成功",
      success: true,
      data: doc,
    });
  } else {
    let res = await Student.create(q.body);
    s.send({
      msg: "注册成功",
      success: true,
      data: res,
    });
  }
});

// 获取其他信息
router.post("/getUserData", async (q, s) => {
  const stu = await Student.findById(q.body.id);
  let school = {};
  let classes = [];
  if (stu.schoolId) {
    school = await School.findById(stu.schoolId);
  }
  classes = await Claxx.findOne({ _id: stu.classId });
  s.send({
    msg: "获取成功",
    success: true,
    data: {
      stu,
      school,
      classes,
    },
  });
});

// 填写其他信息
router.post("/edit", async (q, s) => {
  const account = q.body.account;
  delete q.body.account;
  let res = await Student.updateOne({ account }, q.body); // 更新第一个匹配的，第二个参数为更新的字段内容
  s.send({
    msg: "修改成功",
    success: true,
    data: res,
  });
});

// 学生加入班级-课程
router.post("/joinClass", async (q, s) => { });

// 学生加入班级

// 学生提交作业
router.post("/addScore", async (q, s) => {
  console.log(q.body);
  const res = await Score.create(q.body);
  s.send({
    msg: "成功",
    success: true,
    data: res,
  });
});


module.exports = router;
